<template>
  <div class="p-6 bg-base-200/50 rounded-2xl">
    <CommentItem
      :comment="comment"
      :replies="replies"
      :show-replies="true"
      @toggle-like="handleToggleLike"
      @create-reply="handleCreateReply"
      @report-comment="handleReportComment"
      @delete-comment="handleDeleteComment"
      @reply-click="handleReplyClick"
    >
      <!-- 自定义操作按钮插槽 -->
      <template #actions="{ comment, handlers }">
        <div class="flex items-center gap-4">
          <!-- 自定义点赞按钮 -->
          <button
            @click="handlers.toggleLike"
            class="flex items-center gap-2 px-3 py-2 rounded-full transition-all duration-200 hover:scale-105"
            :class="
              comment.isLiked
                ? 'bg-red-100 text-red-600'
                : 'bg-base-200 text-base-content/60 hover:bg-red-100 hover:text-red-600'
            "
          >
            <svg class="w-4 h-4" fill="currentColor" viewBox="0 0 24 24">
              <path
                d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z"
              />
            </svg>
            <span class="font-semibold">{{ comment.likes }}</span>
          </button>

          <!-- 自定义回复按钮 -->
          <button
            @click="handlers.startReply"
            class="flex items-center gap-2 px-3 py-2 bg-primary/10 text-primary rounded-full hover:bg-primary hover:text-primary-content transition-all duration-200 hover:scale-105"
          >
            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path
                stroke-linecap="round"
                stroke-linejoin="round"
                stroke-width="2"
                d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z"
              ></path>
            </svg>
            <span class="font-semibold">回复</span>
          </button>

          <!-- 分享按钮 -->
          <button
            class="flex items-center gap-2 px-3 py-2 bg-base-200 text-base-content/60 rounded-full hover:bg-base-300 transition-all duration-200 hover:scale-105"
          >
            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path
                stroke-linecap="round"
                stroke-linejoin="round"
                stroke-width="2"
                d="M8.684 13.342C8.886 12.938 9 12.482 9 12c0-.482-.114-.938-.316-1.342m0 2.684a3 3 0 110-2.684m0 2.684l6.632 3.316m-6.632-6l6.632-3.316m0 0a3 3 0 105.367-2.684 3 3 0 00-5.367 2.684zm0 9.316a3 3 0 105.367 2.684 3 3 0 00-5.367-2.684z"
              ></path>
            </svg>
            <span class="font-semibold">分享</span>
          </button>

          <!-- 收藏按钮 -->
          <button
            class="flex items-center gap-2 px-3 py-2 bg-base-200 text-base-content/60 rounded-full hover:bg-yellow-100 hover:text-yellow-600 transition-all duration-200 hover:scale-105"
          >
            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path
                stroke-linecap="round"
                stroke-linejoin="round"
                stroke-width="2"
                d="M4.318 6.318a4.5 4.5 0 000 6.364L12 20.364l7.682-7.682a4.5 4.5 0 00-6.364-6.364L12 7.636l-1.318-1.318a4.5 4.5 0 00-6.364 0z"
              ></path>
            </svg>
            <span class="font-semibold">收藏</span>
          </button>
        </div>
      </template>
    </CommentItem>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { CommentItem } from '@/components/common/comment'
import type { Comment } from '@/apis/methods/comment.api'

// 评论数据
const comment = ref<Comment>({
  id: '1',
  content:
    '这个示例展示了自定义操作按钮插槽的用法。有自定义的点赞、回复、分享、收藏按钮，每个都有不同的样式和悬停效果。',
  author: {
    id: '1',
    name: '按钮专家',
    avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=button',
  },
  createdAt: '2024-01-15T10:30:00Z',
  updatedAt: '2024-01-15T10:30:00Z',
  likes: 16,
  isLiked: false,
  replyCount: 2,
  parentId: undefined,
  articleId: '1',
  mentionedUsers: [],
  status: 'approved',
})

// 回复数据
const replies = ref<Comment[]>([
  {
    id: 'r1',
    content: '按钮设计得很棒！',
    author: {
      id: '2',
      name: '用户',
      avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=user',
    },
    createdAt: '2024-01-15T11:00:00Z',
    updatedAt: '2024-01-15T11:00:00Z',
    likes: 2,
    isLiked: false,
    replyCount: 0,
    parentId: '1',
    articleId: '1',
    mentionedUsers: [],
    status: 'approved',
  },
])

// 事件处理函数
const handleToggleLike = (commentId: string) => {
  const targetComment =
    comment.value.id === commentId ? comment.value : replies.value.find((c) => c.id === commentId)
  if (targetComment) {
    targetComment.isLiked = !targetComment.isLiked
    targetComment.likes += targetComment.isLiked ? 1 : -1
  }
}

const handleCreateReply = (parentId: string, content: string) => {
  console.log('创建回复:', { parentId, content })
}

const handleReportComment = (commentId: string) => {
  console.log('举报评论:', commentId)
}

const handleDeleteComment = (commentId: string) => {
  console.log('删除评论:', commentId)
}

// 这些函数在插槽示例中不需要实际使用，但保留用于演示
// const handleAvatarClick = (author: Comment['author']) => {
//   console.log('点击头像:', author)
// }

// const handleAuthorClick = (author: Comment['author']) => {
//   console.log('点击作者:', author)
// }

// const handleContentClick = (comment: Comment) => {
//   console.log('点击内容:', comment)
// }

const handleReplyClick = (reply: Comment) => {
  console.log('点击回复:', reply)
}
</script>

<style scoped>
/* 自定义操作按钮样式 */
</style>
